﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Drawing2D;

using BrightIdeasSoftware;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoney.Data;

namespace FCNS.KeepMoney.Util
{
    internal class ListViewHotItem : AbstractOverlay
    {
        public ListViewHotItem()
        {
            businessCardRenderer.HeaderBackBrush = Brushes.DarkBlue;
            businessCardRenderer.BorderPen = new Pen(Color.DarkBlue, 2);
            this.Transparency = 255;
        }

        public override void Draw(ObjectListView olv, Graphics g, Rectangle r)
        {
            if (olv.HotRowIndex < 0)
                return;

            if (olv.View == View.Tile)
                return;

            OLVListItem item = olv.GetItem(olv.HotRowIndex);
            if (item == null)
                return;

            Size cardSize = new Size(250, 100);
            Rectangle cardBounds = new Rectangle(
                r.Right - cardSize.Width - 8, r.Bottom - cardSize.Height - 8, cardSize.Width, cardSize.Height);
            businessCardRenderer.DrawBusinessCard(g, cardBounds, olv, item);
        }

        private BusinessCardRenderer businessCardRenderer = new BusinessCardRenderer();
    }

    internal class BusinessCardRenderer : AbstractRenderer
    {
        public override bool RenderItem(DrawListViewItemEventArgs e, Graphics g, Rectangle itemBounds, object rowObject)
        {
            // If we're in any other view than Tile, return false to say that we haven't done
            // the rendereing and the default process should do it's stuff
            ObjectListView olv = e.Item.ListView as ObjectListView;
            if (olv == null || olv.View != View.Tile)
                return false;

            // Use buffered graphics to kill flickers
            BufferedGraphics buffered = BufferedGraphicsManager.Current.Allocate(g, itemBounds);
            g = buffered.Graphics;
            g.Clear(olv.BackColor);
            g.SmoothingMode = ObjectListView.SmoothingMode;
            g.TextRenderingHint = ObjectListView.TextRenderingHint;

            if (e.Item.Selected)
            {
                this.BorderPen = Pens.Blue;
                this.HeaderBackBrush = new SolidBrush(olv.HighlightBackgroundColorOrDefault);
            }
            else
            {
                this.BorderPen = new Pen(Color.FromArgb(0x33, 0x33, 0x33));
                this.HeaderBackBrush = new SolidBrush(Color.FromArgb(0x33, 0x33, 0x33));
            }
            DrawBusinessCard(g, itemBounds, olv, (OLVListItem)e.Item);

            // Finally render the buffered graphics
            buffered.Render();
            buffered.Dispose();

            // Return true to say that we've handled the drawing
            return true;
        }

        internal Pen BorderPen = new Pen(Color.FromArgb(0x33, 0x33, 0x33));
        internal Brush TextBrush = new SolidBrush(Color.FromArgb(0x22, 0x22, 0x22));
        internal Brush HeaderTextBrush = Brushes.Black;
        internal Brush HeaderBackBrush = new SolidBrush(Color.FromArgb(0x33, 0x33, 0x33));
        internal Brush BackBrush = Brushes.LemonChiffon;

        public void DrawBusinessCard(Graphics g, Rectangle itemBounds, ObjectListView olv, OLVListItem item)
        {
            const int spacing = 8;

            // Allow a border around the card
            itemBounds.Inflate(-2, -2);

            // Draw card background
            const int rounding = 20;
            GraphicsPath path = this.GetRoundedRect(itemBounds, rounding);
            g.FillPath(this.BackBrush, path);
            g.DrawPath(this.BorderPen, path);

            g.Clip = new Region(itemBounds);

             //Now draw the text portion
            RectangleF textBoxRect = itemBounds;
            textBoxRect.X += spacing;
            textBoxRect.Y += 5;
            textBoxRect.Width = itemBounds.Right - textBoxRect.X - spacing;

            StringFormat fmt = new StringFormat(StringFormatFlags.NoWrap);
            fmt.Trimming = StringTrimming.EllipsisCharacter;
            fmt.Alignment = StringAlignment.Center;
            fmt.LineAlignment = StringAlignment.Near;

            EntryField field = (EntryField)item.RowObject;
            BankField bField = BankField.GetBankField.Find(bf => { return bf.Id == field.BankId; });
            StringBuilder sb = new StringBuilder();
            sb.Append(LanguageUtil.余额 + bField.Balance+ "\r\n"+LanguageUtil.信用额+bField.Credit + "\r\n\r\n");
            sb.Append(LanguageUtil.当前页面合计);
            decimal de = 0;
            
            foreach (EntryField f in olv.FilteredObjects)
                de += f.Money;

            sb.Append(de);

            using (Font font = new Font("Tahoma", 10))
            {
                // Measure the height of the title
                SizeF size = g.MeasureString(sb.ToString(), font, (int)textBoxRect.Width, fmt);
                // Draw the title
                RectangleF r3 = textBoxRect;
                r3.Height = size.Height;
                path = this.GetRoundedRect(r3, 15);
                g.DrawString(field.columnBankName, font, this.HeaderTextBrush, textBoxRect, fmt);
                fmt.Alignment = StringAlignment.Near;
                textBoxRect.Y += 25;
                g.DrawString(sb.ToString(), font, this.HeaderTextBrush, textBoxRect, fmt);
                fmt.Dispose();
            }
        }

        private GraphicsPath GetRoundedRect(RectangleF rect, float diameter)
        {
            GraphicsPath path = new GraphicsPath();

            RectangleF arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
            path.AddArc(arc, 180, 90);
            arc.X = rect.Right - diameter;
            path.AddArc(arc, 270, 90);
            arc.Y = rect.Bottom - diameter;
            path.AddArc(arc, 0, 90);
            arc.X = rect.Left;
            path.AddArc(arc, 90, 90);
            path.CloseFigure();

            return path;
        }
    }
}
